home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q1082.dms / q1082.adf / src.lzh / Fig / line.c < prev    next >
C/C++ Source or Header  |  1991-07-18  |  9KB  |  367 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : Aug 1985.
  7.  *
  8.  *    %W%    %G%
  9. */
  10. #include "fig.h"
  11. #include "resources.h"
  12. #include "alloc.h"
  13. #include "func.h"
  14. #include "object.h"
  15. #include "paintop.h"
  16.  
  17. extern            (*canvas_kbd_proc)();
  18. extern            (*canvas_locmove_proc)();
  19. extern            (*canvas_leftbut_proc)();
  20. extern            (*canvas_middlebut_proc)();
  21. extern            (*canvas_rightbut_proc)();
  22. extern            null_proc();
  23. extern            set_popupmenu();
  24.  
  25. extern int        fix_x, fix_y, cur_x, cur_y;
  26.  
  27. extern int        cur_command;
  28. extern int        manhattan_mode, mountain_mode;
  29. extern int        latexline_mode, latexarrow_mode;
  30. extern int        magnet_mode, latexarrow_mode;
  31. extern int        autoforwardarrow_mode, autobackwardarrow_mode;
  32. extern int        cur_line_style, line_thickness;
  33. extern float        cur_styleval;
  34. extern int        cur_color;
  35. extern F_compound    objects;
  36. extern int        num_point;
  37. extern int        latex_endpoint();
  38.  
  39. extern F_point        *first_point, *cur_point; 
  40.  
  41. /*************************** locally global variables *********************/
  42.  
  43. static int        firstx, firsty;
  44.             init_line_drawing();
  45.  
  46. int            create_lineobject();
  47.             create_latexobject();
  48.             determine_angle();
  49.             freehand_elasticline();
  50.             latex_elasticline();
  51.             angle0_elasticline(), angle90_elasticline(); 
  52.             angle45_elasticline(), angle135_elasticline(); 
  53.             get_direction(), get_intermediatepoint(),
  54.             get_latexpoint();
  55.  
  56. /**********************  polyline and polygon section  **********************/
  57.  
  58. line_drawing_selected()
  59. {
  60.     canvas_kbd_proc = null_proc;
  61.     canvas_locmove_proc = null_proc;
  62.     canvas_leftbut_proc = init_line_drawing;
  63.     canvas_middlebut_proc = null_proc;
  64.     canvas_rightbut_proc = set_popupmenu;
  65.     set_cursor(&arrow_cursor);
  66.     reset_action_on();
  67.     }
  68.  
  69. init_line_drawing(x, y)
  70. int    x, y;
  71. {
  72.     first_point = Point_malloc(cur_point);
  73.     if (cur_point == NULL) {
  74.         blink_msg();
  75.         put_msg(Err_mem);
  76.         return;
  77.         }
  78.     canvas_rightbut_proc = null_proc;
  79.     set_action_on();
  80.     cur_point->x = firstx = fix_x = cur_x = x;
  81.     cur_point->y = firsty = fix_y = cur_y = y;
  82.     cur_point->next = NULL;
  83.     num_point = 1;
  84.     if (latexline_mode || latexarrow_mode) {
  85.         canvas_locmove_proc = latex_elasticline;
  86.         canvas_leftbut_proc = get_latexpoint;
  87.         canvas_middlebut_proc = create_latexobject;
  88.         }
  89.     else if (manhattan_mode || mountain_mode) {
  90.         canvas_locmove_proc = determine_angle;
  91.         canvas_leftbut_proc = get_direction;
  92.         canvas_middlebut_proc = create_lineobject;
  93.         }
  94.     else {
  95.         canvas_locmove_proc = freehand_elasticline;
  96.         canvas_leftbut_proc = get_intermediatepoint;
  97.         canvas_middlebut_proc = create_lineobject;
  98.         }
  99.     set_temp_cursor(&null_cursor);
  100.     cur_cursor = &null_cursor;
  101.     draw_elasticline();
  102.     }
  103.  
  104. draw_elasticline()
  105. {
  106.     pw_vector(canvas_pixwin, fix_x, fix_y, cur_x, cur_y, INV_PAINT,1);
  107.     }
  108.  
  109. freehand_elasticline(x, y)
  110. int    x, y;
  111. {
  112.     draw_elasticline();
  113.     cur_x = x;  
  114.     cur_y = y;
  115.     draw_elasticline();
  116.     }
  117.  
  118. draw_latexline()
  119. {
  120.     int  x, y;
  121.     CURSOR c;
  122.  
  123.     latex_endpoint(fix_x, fix_y, cur_x, cur_y, &x, &y, latexarrow_mode, (magnet_mode)? 5: 1);
  124.     pw_vector(canvas_pixwin, fix_x, fix_y, x, y, INV_PAINT,1);
  125.     c = (x == cur_x  &&  y == cur_y)? &null_cursor: &crosshair_cursor;
  126.     if (c != cur_cursor) {
  127.         set_temp_cursor(c);
  128.         cur_cursor = c;
  129.         }
  130.     }
  131.  
  132. latex_elasticline(x, y)
  133. int    x, y;
  134. {
  135.     draw_latexline();
  136.     cur_x = x;  
  137.     cur_y = y;
  138.     draw_latexline();
  139.     }
  140.  
  141. angle0_elasticline(x, y)
  142. int    x, y;
  143. {
  144.     if (x == cur_x && y == cur_y) return;
  145.     pw_vector(canvas_pixwin, fix_x, fix_y, cur_x, cur_y, INV_PAINT,1);
  146.     cur_x = x;
  147.     pw_vector(canvas_pixwin, fix_x, fix_y, cur_x, cur_y, INV_PAINT,1);
  148.     win_setmouseposition(canvas_swfd, cur_x, cur_y);
  149.     }
  150.  
  151. angle90_elasticline(x, y)
  152. int    x, y;
  153. {
  154.     if (x == cur_x && y == cur_y) return;
  155.     pw_vector(canvas_pixwin, fix_x, fix_y, cur_x, cur_y, INV_PAINT,1);
  156.     cur_y = y;
  157.     pw_vector(canvas_pixwin, fix_x, fix_y, cur_x, cur_y, INV_PAINT,1);
  158.     win_setmouseposition(canvas_swfd, cur_x, cur_y);
  159.     }
  160.  
  161. angle45_elasticline(x, y)
  162. int    x, y;
  163. {
  164.     if (x == cur_x && y == cur_y) return;
  165.     pw_vector(canvas_pixwin, fix_x, fix_y, cur_x, cur_y, INV_PAINT,1);
  166.     if (abs(x - cur_x) < abs(y - cur_y)) {
  167.         cur_x += cur_y - y;
  168.         cur_y = y;
  169.         }
  170.     else {
  171.         cur_y -= x - cur_x;
  172.         cur_x = x;
  173.         }
  174.     pw_vector(canvas_pixwin, fix_x, fix_y, cur_x, cur_y, INV_PAINT,1);
  175.     win_setmouseposition(canvas_swfd, cur_x, cur_y);
  176.     }
  177.  
  178. angle135_elasticline(x, y)
  179. int    x, y;
  180. {
  181.     if (x == cur_x && y == cur_y) return;
  182.     pw_vector(canvas_pixwin, fix_x, fix_y, cur_x, cur_y, INV_PAINT,1);
  183.     if (abs(x - cur_x) < abs(y - cur_y)) {
  184.         cur_x += y - cur_y;
  185.         cur_y = y;
  186.         }
  187.     else {
  188.         cur_y += x - cur_x;
  189.         cur_x = x;
  190.         }
  191.     pw_vector(canvas_pixwin, fix_x, fix_y, cur_x, cur_y, INV_PAINT,1);
  192.     win_setmouseposition(canvas_swfd, cur_x, cur_y);
  193.     }
  194.  
  195. get_direction(x, y)
  196. int    x, y;
  197. {
  198.     (*canvas_locmove_proc) (x, y);
  199.     canvas_locmove_proc = determine_angle;
  200.     get_intermediatepoint(cur_x, cur_y);
  201.     }
  202.  
  203. determine_angle(x, y)
  204. int    x, y;
  205. {
  206.     float    angle, dx, dy;
  207.  
  208.     dx = x - fix_x;  dy = fix_y - y;
  209.     if (sqrt((double)(dx*dx + dy*dy)) < 7) return;
  210.     if (dx == 0)
  211.         angle = -90;
  212.     else
  213.         angle = 180 * atan((double)(dy / dx)) / 3.1416;
  214.  
  215.     if (manhattan_mode) {
  216.         if (mountain_mode) {
  217.         if (angle < -67.5) canvas_locmove_proc = angle90_elasticline;
  218.         else if (angle < -22.5) canvas_locmove_proc = angle135_elasticline;
  219.         else if (angle <  22.5) canvas_locmove_proc = angle0_elasticline;
  220.         else if (angle <  67.5) canvas_locmove_proc = angle45_elasticline;
  221.         else canvas_locmove_proc = angle90_elasticline;
  222.         }
  223.         else {
  224.         if (angle < -45) canvas_locmove_proc = angle90_elasticline;
  225.         else if (angle < 45) canvas_locmove_proc = angle0_elasticline;
  226.         else canvas_locmove_proc = angle90_elasticline;
  227.         }
  228.         }
  229.     else {
  230.         if (angle < 0) canvas_locmove_proc = angle135_elasticline;
  231.         else canvas_locmove_proc = angle45_elasticline;
  232.         }
  233.         
  234.     (*canvas_locmove_proc)(x, y);
  235.     }
  236.  
  237. get_latexpoint(x, y)
  238. int    x, y;
  239. {
  240.     draw_latexline();
  241.     latex_endpoint(fix_x, fix_y, x, y, &cur_x, &cur_y, latexarrow_mode, (magnet_mode)? 5: 1);
  242.     if (cur_cursor != &null_cursor) {
  243.         set_temp_cursor(&null_cursor);
  244.         cur_cursor = &null_cursor;
  245.         }
  246.     win_setmouseposition(canvas_swfd, cur_x, cur_y);
  247.     get_intermediatepoint(cur_x, cur_y);
  248.     }
  249.  
  250. get_intermediatepoint(x, y)
  251. int    x, y;
  252. {
  253.     draw_elasticline();  /* erase elastic line */
  254.     cur_x = x;
  255.     cur_y = y;
  256.     if (cur_command == F_POLYLINE || cur_command == F_POLYGON)
  257.         draw_line_segment(cur_line_style, cur_styleval, fix_x, fix_y, x, y, 
  258.                 PAINT);
  259.     else
  260.         draw_elasticline();
  261.     num_point++;
  262.     fix_x = x;
  263.     fix_y = y;
  264.     draw_elasticline();
  265.     append_point(fix_x, fix_y, &cur_point);
  266.     }
  267.  
  268. create_latexobject(x, y)
  269. int    x, y;
  270. {
  271.     if (x != fix_x || y != fix_y || num_point == 1) {
  272.         draw_latexline();
  273.         latex_endpoint(fix_x, fix_y, x, y, &cur_x, &cur_y, latexarrow_mode, (magnet_mode)? 5: 1);
  274.         if (cur_cursor != &null_cursor) {
  275.         set_temp_cursor(&null_cursor);
  276.         cur_cursor = &null_cursor;
  277.         }
  278.         win_setmouseposition(canvas_swfd, cur_x, cur_y);
  279.         }
  280.     create_lineobject(cur_x, cur_y);
  281.     }
  282.  
  283. create_lineobject(x, y)
  284. int    x, y;
  285. {
  286.     extern F_arrow    *forward_arrow(), *backward_arrow();
  287.     F_point        *p, *q;
  288.     F_line        *line;
  289.     int        dot;
  290.  
  291.     dot = 0;
  292.     if (x != fix_x || y != fix_y) {
  293.         get_intermediatepoint(x, y);
  294.         draw_elasticline();
  295.         }
  296.     else if (num_point == 1) {
  297.         dot = 1;
  298.         get_intermediatepoint(x, y);
  299.         draw_elasticline();
  300.         }
  301.     if (NULL == (Line_malloc(line))) {
  302.         put_msg(Err_mem);
  303.         line_drawing_selected();
  304.         return;
  305.         }
  306.     line->type = T_POLYLINE;
  307.     line->style = cur_line_style;
  308.     line->thickness = line_thickness;
  309.     line->style_val = cur_styleval;
  310.     line->color = cur_color;
  311.     line->depth = 0;
  312.     line->area_fill = NULL;
  313.     line->pen = NULL;
  314.     line->points = first_point;
  315.     line->next = NULL;
  316.     line->for_arrow = NULL;
  317.     line->back_arrow = NULL;
  318.     if (cur_command == F_POLYGON) {
  319.         if (num_point >= 3) {
  320.         line->type = T_POLYGON;
  321.         num_point++;
  322.         append_point(firstx, firsty, &cur_point);
  323.         draw_line_segment(cur_line_style, cur_styleval, 
  324.             firstx, firsty, fix_x, fix_y, PAINT);
  325.         }
  326.         }
  327.     else if (dot) {
  328.         pw_vector(canvas_pixwin, fix_x, fix_y, cur_x, cur_y, PAINT, 1);
  329.         line->style = SOLID_LINE;
  330.         }
  331.     else if (num_point > 1) {
  332.         if (autoforwardarrow_mode) {
  333.         line->for_arrow = forward_arrow();
  334.         for (p = line->points; p != cur_point; q = p, p = p->next);
  335.         draw_arrow(q->x, q->y, p->x, p->y, line->for_arrow, PAINT);
  336.         }
  337.         if (autobackwardarrow_mode) {
  338.         line->back_arrow = backward_arrow();
  339.         p = line->points->next;
  340.         draw_arrow(p->x, p->y, firstx, firsty, line->back_arrow, PAINT);
  341.         }
  342.         }
  343.     clean_up();
  344.     set_action_object(F_CREATE, O_POLYLINE);
  345.     insert_line(&objects.lines, line);
  346.     set_latestline(line);
  347.     set_modifiedflag();
  348.     line_drawing_selected();
  349.     }
  350.  
  351. append_point(x, y, point)
  352. int    x, y;
  353. F_point    **point;
  354. {
  355.     F_point    *p;
  356.  
  357.     if (NULL == (Point_malloc(p))) {
  358.         put_msg(Err_mem);
  359.         return;
  360.         }
  361.     p->x = x;
  362.     p->y = y;
  363.     p->next = NULL;
  364.     (*point)->next = p;
  365.     *point = p;
  366.     }
  367.